# TokenName_Print.gs :   打印宏名字, 断点位置 来自于 watch_Preprocessor_NumDefined_then_User_h_MacroName.gs
# 执行:  cd /pubx/llvm-project/analyze_clang_by_gdb/;  gdb -x TokenName_Print.gs    --args /pubx/llvm15-build/bin/clang    -c user_main.cpp -o user_main.o
# 大约十几分钟之后才会有打印输出(此时执行到了User.h)
# grep -i  expand TokenName_Print.log   #执行完后, 在日志中搜索 展开expand, 有如下函数. 即将 寻找宏展开处 ExpandMacro.gs
#2  0x0000555563154bfb in clang::Preprocessor::HandleMacroExpandedIdentifier (this=0x55556375bcf0, Identifier=..., M=...) at /pubx/llvm-project/clang/lib/Lex/PPMacroExpansion.cpp:477
#2  0x0000555563154bfb in clang::Preprocessor::HandleMacroExpandedIdentifier (this=0x55556375bcf0, Identifier=..., M=...) at /pubx/llvm-project/clang/lib/Lex/PPMacroExpansion.cpp:477
#1  0x00005555631551e3 in clang::Preprocessor::HandleMacroExpandedIdentifier (this=0x55556375bcf0, Identifier=..., M=...) at /pubx/llvm-project/clang/lib/Lex/PPMacroExpansion.cpp:582
#2  0x00005555631551f9 in clang::Preprocessor::HandleMacroExpandedIdentifier (this=0x55556375bcf0, Identifier=..., M=...) at /pubx/llvm-project/clang/lib/Lex/PPMacroExpansion.cpp:582
#2  0x0000555563154bfb in clang::Preprocessor::HandleMacroExpandedIdentifier (this=0x55556375bcf0, Identifier=..., M=...) at /pubx/llvm-project/clang/lib/Lex/PPMacroExpansion.cpp:477

set logging file TokenName_Print.log
set logging on

set print pretty on
set pagination off

set follow-exec-mode new
set follow-fork-mode child

set breakpoint pending on

# 自定义gdb命令 create_cstr_with_len_then_print  $_srcCStr  $_copyLength  $_title:  复制来源字符串开头的给定长度字符串 并打印
source create_cstr_with_len_then_print.gs


#人工 从 Twine_union_Child_print.gs 中 应该去  函数 createPreprocessor 
break CompilerInstance.cpp:473
#断点1 clang::CompilerInstance::createPreprocessor ( ) at /pubx/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:473
commands 1
	disable 1

	frame 2
	#2  0x000055555e12011f in clang::CompilerInstance::ExecuteAction (this=0x555563747760, Act=...) at /pubx/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1034
	set $ciPtr=this
	set $smPtr=& ( $ciPtr->getSourceManager() )

	frame 0
	#0  clang::CompilerInstance::createPreprocessor (this=0x555563747760, TUKind=clang::TU_Complete) at /pubx/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:473
	set $boolPtr=(bool*)(malloc(sizeof(bool)))

#getIdentifierInfo isAnyIdentifier is
	break clang::Token::getIdentifierInfo  if ($_fileNamCStr=$smPtr->getFilename(this->getLocation () ).Data) != 0 && strcmp("./User.h",$_fileNamCStr)==0
	# if       $smPtr->getFilename(  this->getLocation () )  .Data  != 0 && strcmp("./User.h",  $smPtr->getFilename(  this->getLocation () )  .Data  )==0 
	#条件断点可以极大降低 目标应用程序在断点上消耗的时间
	commands 2
		silent
		set $_tokenLocation=this->getLocation ()
		set $_tokenLength=this->getLength()
		# print $bufferData
		# if $macroNameCStr!=0 && strcmp("ADMIN_USER_ID",$macroNameCStr)==0
			# print "断点2 停止后  感兴趣宏名字  需要停下"
			
			#std::pair<FileID, unsigned>  getDecomposedLoc (... )
			set $fileIdAndTokenIndex= $smPtr->getDecomposedLoc($_tokenLocation)

			set $fileId=$fileIdAndTokenIndex.first
			#$fileIdAndTokenIndex.first 和 $valueToken0_fileIdAndTokenPosition.first 是一样的, 即 文件id一样, 都在同一个源代码文件中
			set $tokenIdx=$fileIdAndTokenIndex.second

			set $bufferData= $smPtr->getBufferData($fileId,$boolPtr)
			set $bufferDataCStr=$bufferData.Data

			#TODO: 宏名修复:   "宏名:PI (3.14"
			#构造宏名字字符串, 打印, 释放
			create_cstr_with_len_then_print  $bufferDataCStr+$tokenIdx  $_tokenLength "token:"

			#打印当前位置部分代码
			print $smPtr->getCharacterData($_tokenLocation,$boolPtr)
			
			#打印调用栈
			bt
			

		# else
			# continue
			#断点2 停止后 非感兴趣宏名字 不需要停下
		# end
		#if结束

		
		#若想在User.h中感兴趣的宏处停下, 注释掉紧挨着的continue即可， 在停下后多次手工continue 即可到达想要的宏处
		continue
		#断点2 停止后   不需要停下
	end
	#断点2处理结束

continue
#断点1 停止后 不需要停下
end
#断点1 处理结束


run

#因断点1 而停下
